package com.bytecollege.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytecollege.model.Course;
import com.bytecollege.model.Ctype;
import com.bytecollege.model.Notice;
import com.bytecollege.service.Impl.CourseServiceImpl;
import com.bytecollege.service.Impl.CtypeServiceImpl;
import com.bytecollege.service.Impl.NoticeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    CourseServiceImpl courseService;

    @Autowired
    CtypeServiceImpl ctypeService;
    @Autowired
    NoticeServiceImpl noticeService;

    @GetMapping({"", "/index"})
    public String index(Model model) {
        Page<Course> page = new Page<>(1, 4);
        //根据点击量取最多的4个
        List<Course> hotCourse = courseService.getSearch(false, null, false, null, false, null, true, "course_clicks", page);
        model.addAttribute("hotCourse", hotCourse);
        //根据时间取最新的4个
        List<Course> newCourse = courseService.getSearch(false, null, false, null, false, null, true, "course_time", page);
        model.addAttribute("newCourse", newCourse);
        //找出免费课程4个
        List<Course> freeCourse = courseService.getSearch(false, null, true, "0", false, null, false, null, page);
        model.addAttribute("freeCourse", freeCourse);
        //找出vip课程4个
        List<Course> vipCourse = courseService.getSearch(false, null, true, "1", false, null, false, null, page);
        model.addAttribute("vipCourse", vipCourse);

        //所有课程
        List<Ctype> ctypeList = ctypeService.getCtype();
        model.addAttribute("ctypeList", ctypeList);
        //所有公告
        List<Notice> noticeList = noticeService.getAll();
        model.addAttribute("noticeList",noticeList);
        return "index";
    }


    /**
     * 搜索和关键词过滤
     *
     * @param pageIndex 页面索引
     * @param pageSize  页面大小
     * @param search    搜索内容
     * @param filed     vip：1,最新：new，最热:hot,免费：0
     * @param typeId    typeId -1不使用该查询
     * @param model
     * @return result页面
     */
    @GetMapping("/course/search")
    public String getSearch(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                            @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                            @RequestParam(value = "search", defaultValue = "") String search,
                            @RequestParam(value = "filed", defaultValue = "-1") String filed,
                            @RequestParam(value = "typeId", defaultValue = "-1") String typeId,
                            Model model) {
        //分页
        Page<Course> page = new Page<>(pageIndex, pageSize);
        //是否根据typeId查询
        boolean isType = true;
        //是否查询vip状态
        boolean isVip = false;
        //是否使用搜索
        boolean isSearch = true;
        //是否使用排序
        boolean isOrder = false;
        //字段映射
        String filedValue;
        //filed 0,1使用vip查询
        if (filed.equals("0") || filed.equals("1")) {
            isVip = true;
        }
        //-1不根据typeId查询
        if (typeId.equals("-1")) {
            isType = false;
        }
        //字段映射，开启排序
        switch (filed) {
            case "new":
                filedValue = "course_time";
                isOrder = true;
                break;
            case "hot":
                filedValue = "course_clicks";
                isOrder = true;
                break;
            default:
                filedValue = filed;
        }
        //空串不使用查询
        if (search.equals("")) {
            isSearch = false;
        }
        List<Ctype> typeList = ctypeService.getCtype();
        List<Course> courseList = courseService.getSearch(isSearch, search, isVip, filedValue, isType, typeId, isOrder, filedValue, page);
        model.addAttribute("filed", filed);
        model.addAttribute("typeId", typeId);
        model.addAttribute("search", search);
        model.addAttribute("page", page);
        model.addAttribute("courseList", courseList);
        model.addAttribute("typeList", typeList);
        return "/course/result";
    }

    @GetMapping("/toNotice")
    public String toNotice(int id,Model model){
        Notice notice = noticeService.getById(id);
        model.addAttribute("notice",notice);
        return "notice";
    }

    @GetMapping("/toUpload")
    public String toUpload(){
        return "uploadImageExample";
    }


    @GetMapping("/toVip")
    public String toVip(){
        return "vip";
    }
}
